package uit.minito.timduongxebuyt;

import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Vector;

import uit.minito.database.HistoryModel;
import uit.minito.views.DateTimeUtils;
import uit.minito.views.MyUtilities;
import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class HistoryArrayAdapter extends ArrayAdapter<HistoryModel>{
	Activity context;
	private ArrayList<HistoryModel> historyList;
	public HistoryArrayAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		// TODO Auto-generated constructor stub  

	}
	public HistoryArrayAdapter(Activity context, ArrayList<HistoryModel> modelList){
		super(context, android.R.layout.simple_list_item_1);
		this.context=context;
		this.setHistoryList(modelList);
	}

	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View rowView = convertView;
	    if (rowView == null) {
	      LayoutInflater inflater = this.context.getLayoutInflater();
	      rowView = inflater.inflate(R.layout.history_cell, null);  
	    }
	    TextView addressStartTV=(TextView)rowView.findViewById(R.id.addressStartTextView);
	    TextView addressEndTV=(TextView)rowView.findViewById(R.id.addressEndTextView);
	    TextView dateAddedTV=(TextView)rowView.findViewById(R.id.dateAddedTextView);
	    TextView routerDetailsTV=(TextView)rowView.findViewById(R.id.historyRouterDetailsTV);
	    TextView distanceTV=(TextView)rowView.findViewById(R.id.historyDistanceTV);
	    TextView timeSpentTV=(TextView)rowView.findViewById(R.id.historyTimeSpentTV);
	    HistoryModel history=getHistoryList().get(position);
	    addressStartTV.setText(history.getAddressStart());
	    addressEndTV.setText(history.getAddressEnd());
	    dateAddedTV.setText(DateTimeUtils.getInstance(context).getTimeDiffString(history.getDateAdded().getTime()));
	    
	    //set routerDetails
	    String tuyenString=getContext().getResources().getString(R.string.result_router_tuyen);
		String vaString=getContext().getResources().getString(R.string.result_router_va);
		String diString=getContext().getResources().getString(R.string.result_router_di); 
	    String rawRouterDetails=history.getRouterDetails();
	    String finalRouterDetailsString=diString+" "+MyUtilities.makeBoldAndBiggerText(String.valueOf(history.getNumOfRouters()))+" "+tuyenString+" : ";
	    StringTokenizer tokens = new StringTokenizer(rawRouterDetails, ",");
	    Vector<String> colorArray=new Vector<String>();
	    colorArray.add("red");
	    colorArray.add("green");
	    colorArray.add("blue");  
	    int tokenSize=tokens.countTokens();
	    finalRouterDetailsString+=MyUtilities.makeColorToText(MyUtilities.makeBoldAndBiggerText(tokens.nextToken()),colorArray.get(0));
	    
	    for(int j=1;j<tokenSize;j++){
	    	String iTokenString=tokens.nextToken();
	    	if(j==tokenSize-1){
	    		finalRouterDetailsString+=" "+vaString+" "+ MyUtilities.makeColorToText(MyUtilities.makeBoldAndBiggerText(iTokenString),colorArray.get(j));
	    	}else{
	    		finalRouterDetailsString+=", "+MyUtilities.makeColorToText(MyUtilities.makeBoldAndBiggerText(iTokenString),colorArray.get(j));
	    	}
	    	
	    }
	    routerDetailsTV.setText(Html.fromHtml(finalRouterDetailsString));
	    
	    //set  distanceTextView
	    String kmFromRes=getContext().getResources().getString(R.string.result_km);
		String distanceString=MyUtilities.makeBoldAndBiggerText( String.valueOf(MyUtilities.roundDouble(history.getDistance(),1)))+" "+kmFromRes;
	    distanceTV.setText(Html.fromHtml(distanceString));
	    
	    
	    //set timeSpent TextView
	    String phutString=getContext().getResources().getString(R.string.result_phut);
	    String timeSpentString=MyUtilities.makeBoldAndBiggerText(MyUtilities.removeDotZeroFromDouble(history.getTimeSpent())) +" "+phutString;
	    timeSpentTV.setText(Html.fromHtml(timeSpentString));
		return rowView;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return getHistoryList().size();
	}
	public void setHistoryList(ArrayList<HistoryModel> historyList) {
		this.historyList = historyList;
	}
	public ArrayList<HistoryModel> getHistoryList() {
		return historyList;
	}

}
